package com.company.app.action;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.company.app.model.ProductModel;
import com.company.app.service.ProductService;


//
/**
 * The Class ProductAction.
 */
public class ProductAction extends AbstractBaseAction<ProductModel>  {
	
    private static final Logger LOG = LoggerFactory.getLogger(ProductAction.class);
	
	/** The product service. */
	@SpringBean("productServiceClient")
	private ProductService productService;
	
	//TODO IMPLEMENT VALIDATIONS
	
	/** The Constant INIT_VIEW. */
	private static final String INIT_VIEW = "/jsp/product.jsp";
	
	/**
	 * Default method.
	 *
	 * @return the resolution
	 */
	@DefaultHandler  
    public Resolution defaultMethod() {
		return new ForwardResolution(INIT_VIEW);  
    }
	
    /**
     * Save product.
     *
     * @return the resolution
     */
    public Resolution saveProduct() {
    		/*
    		 *** TODO IMPLEMENT STRIPES VALIDATIONS
    		 *  
    		 * if (this.getModel().getProductView() == null) {
                ValidationError error = new LocalizableError("com.company.app.action.ProductAction.model.productView");
                this.getContext().getValidationErrors().add("user", error);
                return this.getContext().getSourcePageResolution();
            }
    		if (this.getModel().getProductView().getCode()==null){
    			ValidationError error = new LocalizableError("com.company.app.action.ProductAction.model.productView.code");
    			this.getContext().getValidationErrors().add("code", error);
                return this.getContext().getSourcePageResolution();
    		}*/
    		if (!this.isProductValid()){
    			this.getContext().getResponse().setStatus(501);
    			LOG.error("Product invalid");
	    	}else{
	    		if (this.getModel().getProductView().getId()!=null){
	    			this.productService.updateProduct(this.getModel().getProductView());
	    		}else{
	    			this.productService.createProduct(this.getModel().getProductView());
	    		}
	    	}
    	//
		return new ForwardResolution(INIT_VIEW);  
    }
    
    public Resolution deleteProduct(){
		/*
		 *** TODO IMPLEMENT STRIPES VALIDATIONS
		 *  
		 * if (this.getModel().getProductView().getId() == null) {
            ValidationError error = new LocalizableError("com.company.app.action.ProductAction.model.productView");
            this.getContext().getValidationErrors().add("user", error);
            return this.getContext().getSourcePageResolution();
        }*/
    	int responseStatus=0;
    	if (this.getModel().getProductView().getId()!=null){
    		if(!this.productService.deleteProduct(this.getModel().getProductView())){
    			responseStatus=500;
    		}
    	}
    	if(responseStatus>0){ this.getContext().getResponse().setStatus(responseStatus);}
    	return new ForwardResolution(INIT_VIEW);
    }
    
    public boolean isProductValid(){
    	boolean isValid = true;
    	if (this.getModel().getProductView() == null ){
    		isValid=false;
    	}else{
    		if (this.getModel().getProductView().getCode()==null){
    			isValid=false;
    		}
    	}
    	return isValid;
    		
    }

    /**
	 * Sets the product service.
	 *
	 * @param productService the new product service
	 */
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
    
}
